#define _CRT_SECURE_NO_WARNINGS



#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef pair<int, int> PII;
const int N = 1010;
int n, m;
int a[N][N];
int dist[N][N];
int d[] = { -1,0,1,0,-1 };
void bfs()
{
    memset(dist, -1, sizeof dist);

    queue<PII> q;
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            if (a[i][j] == 1)
            {
                dist[i][j] = 0;
                q.push({ i,j });
            }
        }
    }

    while (q.size())
    { 
        auto t = q.front();q.pop();
        int i = t.first;
        int j = t.second;
        for (int k = 0;k < 4;k++)
        {
            int x = i + d[k];
            int y = j + d[k + 1];
            if (x >= 1 && x <= n && y >= 1 && y <= m && dist[x][y] == -1)
            {
                dist[x][y] = dist[i][j] + 1;
                q.push({ x,y });
            }
        }
       
    }
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= m; j++)
			cin >> a[i][j];
	}

    bfs();

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++)
            cout << dist[i][j] << " ";
        cout << "\n";
    }

	return 0;
}

